package xiong.copy.rest;



import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import xiong.copy.domain.ColumnInfo;
import xiong.copy.exception.BadRequestException;
import xiong.copy.service.GenConfigService;
import xiong.copy.service.GeneratorService;
import xiong.copy.utils.PageUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/generator")
@Api(tags = "系统：代码生成类")
public class GeneratorController {



    private final GeneratorService generatorService;
    private final GenConfigService genConfigService;

    @Value("${generator.enabled}")
    private Boolean generatorEnabled;


    /**
     * swagger 注解
     * @Url /tables/all
     * @result ResponseEntity<Object>
     */

    @ApiOperation("查询数据库")
    @GetMapping(value = "/tables/all")
    public ResponseEntity<Object> queryTables (){
        return new ResponseEntity<>(generatorService.getTables(),HttpStatus.OK);
    }

    /**
     * 分页
     * @param name
     * @param page 页码
     * @param size 单页条目数
     *
     * 使用 PageUtil 分页 求范围   继承了Hutool的PageUtil
     * RequestParam（default=""） 设置默认值
     *   ？？？ 这里默认值为"10"  Integer 能接收吗
     */

    @ApiOperation("查询数据库数据")
    @GetMapping(value = "/tables")
    public ResponseEntity<Object> get1(@RequestParam(defaultValue = "",value = "name") String name ,
                                       @RequestParam(defaultValue = "0",value = "page") Integer page,
                                       @RequestParam(defaultValue = "10",value = "size") Integer size){
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>( generatorService.getTables(name,startEnd) ,HttpStatus.OK);
    }

    /**
     *
     *
     * 使用 自定义的PageUtil 分页 存入Map中
     */

    @ApiOperation("查询字段数据")
    @GetMapping(value = "/columns")
    public ResponseEntity<Object> queryColumns(@RequestParam String tableName){
        List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
        return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
    }


    /**
     * putMapping
     * 只要返回状态码
     * 传入参数是对象
     * 用@RequestBody
     * */
    @ApiOperation("保存字段数据")
    @PutMapping
    public ResponseEntity<HttpStatus> save(@RequestBody List<ColumnInfo> columnInfos ){
        generatorService.save(columnInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "sync")
    public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables){
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 生成代码  pathVariable
     * type 值
     * 0 并且 Enabled == false 不能生产
     * 0 generator 生产代码
     * 1  preview 预览
     * 2  download 下载
     * */
    @ApiOperation("生成代码")
    @RequestMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> genrator(@PathVariable("tableName") String tableName,
                                           @PathVariable("type") Integer type,
                                           HttpServletRequest request, HttpServletResponse response){

        if (!generatorEnabled && type ==0){
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }


        switch (type)
        {
            //生产
            case 0:
                generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
                break;
                //预览
            case 1:
                 return    generatorService.preview(genConfigService.find(tableName),generatorService.getColumns(tableName));
            case 2:
                  generatorService.download(genConfigService.find(tableName),generatorService.getColumns(tableName), request,  response);
                break;
            default: throw new BadRequestException("生产代码generation 没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }











}
